package com.example.xm.service.impl;

import com.example.xm.entity.DingCai;
import com.example.xm.entity.Fenlei;
import com.example.xm.entity.Video;
import com.example.xm.exception.SerceiceException;
import com.example.xm.mapper.DingcaiMapper;
import com.example.xm.mapper.FenleiMapper;
import com.example.xm.mapper.VideoMapper;
import com.example.xm.service.IFenleiService;
import com.example.xm.service.IVideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class VideoService implements IVideoService {
    @Autowired
    VideoMapper shipinfenleiMapper;
    @Autowired
    FenleiMapper fenleiMapper;
    @Autowired
    VideoMapper videoMapper;
    @Override
    public void reg(Video fenlei) {
        Video userByName= shipinfenleiMapper.selectByName(fenlei.getName());
//        System.out.println("userByName"+userByName);
        if(userByName!=null){
            throw new SerceiceException(400,"名称已存在");
        }
       String name= fenleiMapper.selectNameById(fenlei.getCid());
        fenlei.setCname(name);
        shipinfenleiMapper.insert(fenlei);
    }

    @Override
    public PageInfo<Video> list(String name,Integer page,Integer size,Integer uid,Integer cur,Integer cid) {
        PageHelper.startPage(page,size);
        List<Video> adminList=  shipinfenleiMapper.selectList(name,cur,cid);
        PageInfo<Video> adminPageInfo = new PageInfo<Video>(adminList);
        return adminPageInfo;
    }

    @Override
    public void update(Video gonggao) {

        String name= fenleiMapper.selectNameById(gonggao.getCid());
        gonggao.setCname(name);
        shipinfenleiMapper.updateByPrimaryKeySelective(gonggao);
    }

    @Override
    public void delete(int[] intArray) {
        shipinfenleiMapper.delete(intArray);
    }
@Autowired
    DingcaiMapper dingcaiMapper;
    @Override
    public Video getById(Integer vid,Integer uid) {
        Video byId = shipinfenleiMapper.getById(vid);
        if(uid!=null){
            //设置收藏状态
            List<DingCai> list = dingcaiMapper.selectList(uid, vid, 3);
            if(list!=null&&list.size()>0){
                byId.setIsShoucang(true);
            }else{
                byId.setIsShoucang(false);
            }
            //设置顶踩状态
            List<DingCai> list1 = dingcaiMapper.selectList(uid, vid, 1);
            if(list1!=null&&list1.size()>0){
                byId.setIsDing(true);
            }
            List<DingCai> list2 = dingcaiMapper.selectList(uid, vid, 2);
            if(list2!=null&&list2.size()>0){
                byId.setIsCai(true);
            }
            //添加历史记录
            List<DingCai> list3 = dingcaiMapper.selectList(uid, vid, 4);
            Video byId1 = videoMapper.getById(vid);
            DingCai dingCai = new DingCai();
            dingCai.setUid(uid);
            dingCai.setVid(vid);
            dingCai.setType(4);
            dingCai.setCid(byId1.getCid());
            if(list3!=null&&list3.size()>0){

                dingcaiMapper.del(dingCai);
            }
            dingcaiMapper.add(dingCai);
        }
      return   byId;
    }

    @Override
    public List<Video> getTuijian(Integer uid) {
        if(uid==null){
          return   videoMapper.selectList(null, null, null);
        }
        //拿到用户的记录分类
     List<Integer> categoryList=   videoMapper.selectCategoryList(uid);
     //拿到看过此分类的用户
        List<Integer> userList=   videoMapper.selectUserList(categoryList);
        //拿到分类
        List<Integer> cids=   videoMapper.selectCidList(userList);
        List<Video> list=   videoMapper.selectVideoListByCIds(cids);
        if(list!=null&&list.size()>0){
        return list;

        }else{
            return   videoMapper.selectList(null, null, null);
        }
    }
}
